package leetcode.editor.cn;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class _17_LetterCombinationsOfAPhoneNumber {
    public static void main(String[] args) {
        Solution solution = new _17_LetterCombinationsOfAPhoneNumber().new Solution();
        List<String> strings = solution.letterCombinations("23");
        for (String string : strings) {
            System.out.println(string);
        }
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        private Map<Integer, String> map = new HashMap<Integer, String>() {
            {
                put(2, "abc");
            }

            {
                put(3, "def");
            }

            {
                put(4, "ghi");
            }

            {
                put(5, "jkl");
            }

            {
                put(6, "mno");
            }

            {
                put(7, "pqrs");
            }

            {
                put(8, "tuv");
            }

            {
                put(9, "wxyz");
            }
        };

        private List<String> letterCombinations(String digits, int idx) {
            int cur = digits.charAt(idx) - '0';
            String s = map.get(cur);
            List<String> res = new ArrayList<>();
            if (idx == digits.length() - 1) {
                for (char c : s.toCharArray()) {
                    res.add(String.valueOf(c));
                }
            } else {
                List<String> left = letterCombinations(digits, idx + 1);
                for (char c : s.toCharArray()) {
                    for (String s1 : left) {
                        res.add(c + s1);
                    }
                }
            }
            return res;
        }


        public List<String> letterCombinations(String digits) {
            if (digits.equals("")) {
                return new ArrayList<String>();
            }
            return letterCombinations(digits, 0);
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}